var express = require('express');
var router = express.Router();

const accountModel = require('../data/model/accountModel')

router.get('/account', function (req, res, next) {
    accountModel.find().sort({time:-1}).then(data => {
        res.render('list', { accounts: data })
    }).catch(err => {
        res.status(500).send(err)
    })
});

router.post('/account', (req, res) => {
    accountModel.create({ ...req.body }).then(data => {
        res.render('success', { msg: "添加成功", url: '/account' })
    }).catch(err => {
        res.status(500).send(err)
    })
})

router.get('/account:id', (req, res) => {
    accountModel.deleteOne({ _id: req.params.id.trim() }).then(data => {
        if (!data.deletedCount)
            res.send('删除失败')
        res.render('success', { msg: "删除成功", url: '/account' })
    }).catch(err => {
        res.status(500).send(err)
    })
})

router.get('/account/create', function (req, res, next) {
    res.render('create');
});



module.exports = router;
